/**
 * 角色管理接口
 * @author lms 2022.08.02
 */
package ms.core.gateway.service;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ms.core.common.*;
import ms.core.common.consts.SysErr;
import ms.core.gateway.request.*;
import ms.core.gateway.response.SysRoleVO;
import ms.core.gateway.tool.SessionTool;
import ms.core.gateway.utils.SysRoleUtils;
import ms.core.tool.*;

public class SysRoleService {

	/**
	 * 接口：<div style="color:red"><b> newRole </b></div><p>
	 * 功能：新增角色<p>
	 * 
	 * @param p ={CreateRoleRequest 对象}<p>
	 * @return ApiResult对象
	 */
	public ApiResult newRole(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid){
		CreateRoleRequest role = CreateRoleRequest.getInstance(body, sid);
	
		SysRoleUtils utils = new SysRoleUtils();
		SysRoleVO ret = utils.newRole(role);
		return new ApiResult(true).setData(ret);
	}
	
	/**
	 * 接口：<div style="color:red"><b> savRole </b></div><p>
	 * 功能：修改角色<p>
	 * 
	 * @param p ={UpdateRoleRequest 对象}<p>
	 * @return ApiResult对象
	 */
	public ApiResult edtRole(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid){
		UpdateRoleRequest role = UpdateRoleRequest.getInstance(body, sid);

		String corpUid = null;
		if (!SessionTool.isSuperCorp(sid)) corpUid = SessionTool.getCorpUid(sid);

		SysRoleUtils utils = new SysRoleUtils();
		SysRoleVO ret = utils.editRole(corpUid, role);
		return new ApiResult(true).setData(ret);
	}

	/**
	 * 接口：<div style="color:red"><b> delRole </b></div><p>
	 * 功能：删除角色<p>
	 * 
	 * @param p ={"id":记录Id}<p>
	 * @return ApiResult对象
	 */
	public ApiResult delRole(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid){
		RequestBody rb = RequestBody.getInstance(body);
		long id = rb.getLongValue("id", 0);
		if (id<=0)
			return new ApiResult(false, SysErr.SYS_PARAM_ERR);

		String corpUid = null;
		if (!SessionTool.isSuperCorp(sid)) 
			corpUid = SessionTool.getCorpUid(sid);

		SysRoleUtils utils = new SysRoleUtils();
		utils.deleteRole(corpUid, id);
		return new ApiResult(true);
	}

	/**
	 * 接口：<div style="color:red"><b> athFun </b></div><p>
	 * 功能：角色授权<p>
	 * 
	 * @param p ={"id":记录Id,"funcs":["funA","funB",...]}<p>
	 * @return ApiResult对象
	 */
	public ApiResult authFuncs(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid){
		RequestBody rb = RequestBody.getInstance(body);
		long id = rb.getLongValue("id", 0);
		List<String> funcs = JsonTool.jsonToListBean(rb.get("funcs"), String.class);
		if (id<1 || funcs==null || funcs.size()==0)
			return new ApiResult(false, SysErr.SYS_PARAM_ERR);

		String corpUid = SessionTool.getCorpUid(sid);
		SysRoleUtils utils = new SysRoleUtils();
		utils.authRoleFuncs(corpUid, id, funcs);

		return new ApiResult(true);
	}

	/**
	 * 接口：<div style="color:red"><b> qryRole </b></div><p>
	 * 功能：角色查询<p>
	 * 
	 * @param p={QueryRoleRequest 对象}<p>
	 * @return ApiResult对象
	 */
	public ApiResult queryRole(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid){
		QueryRoleRequest qry = JsonTool.objectToBean(body, QueryRoleRequest.class);
		if (qry==null)
			throw new AppException(SysErr.SYS_PARAM_ERR, "查询对象不可为空.");

		if (!SessionTool.isSuperCorp(sid) || StrTool.isBlank(qry.getCorpUid()))
			qry.setCorpUid(SessionTool.getCorpUid(sid));

		SysRoleUtils utils = new SysRoleUtils();
		Page<SysRoleVO> pg = utils.queryRole(qry);
		return new ApiResult(true).setPage(pg);
	}
}
